/*
 * Copyright (c) Created by T C L on 2019.7.6.
 */

package cn.mumzone.pos.ui.dialog;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.mumzone.basic.util.AppDateUtil;
import cn.mumzone.basic.util.DisplayUtil;
import cn.mumzone.basic.util.IsFastClickUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.basic.util.ToastUtil;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.SalesTimesCard;
import cn.mumzone.pos.model.TimeCardEntity;
import cn.mumzone.pos.presenter.TimesCardDetailPresenter;
import cn.mumzone.pos.presenter.contract.TimesCardDetailContract;
import cn.mumzone.pos.ui.adapter.ADA_CardUseRecord;
import cn.mumzone.pos.util.GoodsUtil;

public class TimesCardUnavailableDetailDialogFragment extends DialogFragment implements TimesCardDetailContract.View {
    @BindView(R.id.tv_card_name)
    TextView tvCardName;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.tv_card_des)
    TextView tvCardDes;
    @BindView(R.id.tv_card_use_info)
    TextView tvCardUseInfo;
    @BindView(R.id.tv_card_last_time)
    TextView tvCardLastTime;
    @BindView(R.id.rlv_card_use_record)
    RecyclerView rlvCardUseRecord;
    @BindView(R.id.tv_use_time)
    TextView tvUseTime;

    private TimesCardDetailPresenter presenter;

    private String cardId;

    private TimeCardEntity timeCardEntity;
    private TimeCardEntity oldTimeCard;
    private String memberPhone;

    @Override
    public void onStart() {
        super.onStart();
        getDialog().setCanceledOnTouchOutside(false);//点击外部不消失
        // 设置SelectPicPopupWindow弹出窗体的背景
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        window.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.transparent)));
        params.gravity = Gravity.CENTER;
        params.width = DisplayUtil.dip2px(getActivity(), 655);
        params.height = DisplayUtil.dip2px(getActivity(), 400);
        window.setAttributes(params);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = new TimesCardDetailPresenter(getActivity(), this);
        cardId = getArguments().getString("cardId");
        memberPhone = getArguments().getString("memberPhone");
        oldTimeCard = (TimeCardEntity) getArguments().getSerializable("timeCardEntity");
        Log.e("onCreate: ", (oldTimeCard == null) + "");
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_times_card_unavalable_detail, container, false);
        ButterKnife.bind(this, view);
        rlvCardUseRecord.setLayoutManager(new LinearLayoutManager(getActivity()));
        presenter.requestCardConsumeInfo(cardId);
        return view;
    }

    @Override
    public void setCardConsumeInfo(TimeCardEntity timeCardEntity) {
        this.timeCardEntity = timeCardEntity;
        tvCardName.setText(timeCardEntity.getGoodsName());
        tvCardDes.setText("含：" + timeCardEntity.getServiceName());
        tvCardUseInfo.setText(String.format("%s/%s次",
                StringUtils.get(timeCardEntity.getResidueTimes()),
                StringUtils.get(timeCardEntity.getTimes())));
        tvCardLastTime.setText(String.format("有效期至：%s",
                AppDateUtil.getTimeStamp(timeCardEntity.getEndDate(),
                        AppDateUtil.YYYY_MM_DD2)));
        tvUseTime.setVisibility(GoodsUtil.isLimitTimeCard(this.timeCardEntity) ? View.VISIBLE : View.GONE);
        tvUseTime.setText(GoodsUtil.getFormatLimitInfo(this.timeCardEntity));

        switch (timeCardEntity.getStatus()) {
            case 0:
                tvStatus.setText("正常");
                break;
            case 1:
                tvStatus.setText("已用完");
                break;
            case 2:
                tvStatus.setText("已过期");
                break;
            case 3:
                tvStatus.setText("已退卡");
                break;
        }

        ADA_CardUseRecord cardUseRecordAdapter = new ADA_CardUseRecord(false);
        rlvCardUseRecord.setAdapter(cardUseRecordAdapter);
        cardUseRecordAdapter.setDataSet(timeCardEntity.getUsedDetail());
    }

    @OnClick(value = {R.id.iv_close, R.id.tv_buy_again})
    public void toggleEvent(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                dismiss();
                break;
            case R.id.tv_buy_again:
                if(IsFastClickUtil.isFastClick(view)){
                    return;
                }
                if (timeCardEntity == null) {
                    ToastUtil.makeText(getActivity(), "获取次卡信息失败，请退出重试", false);
                    return;
                }

                //组装数据
                SalesTimesCard salesTimesCard = new SalesTimesCard();
                salesTimesCard.setId(timeCardEntity.getGoodsId());
                salesTimesCard.setName(timeCardEntity.getGoodsName());
                salesTimesCard.setServicesName(timeCardEntity.getServiceName());
                salesTimesCard.setTimes(timeCardEntity.getTimes());
                salesTimesCard.setEndDate(timeCardEntity.getEndDate());
                salesTimesCard.setOriginPrice(String.valueOf(timeCardEntity.getTicketPrice()));
                salesTimesCard.setPrice(String.valueOf(timeCardEntity.getTicketPrice()));
                //7-24 本行代码引起崩溃，空指针， 查看代码后不理解为什么要用oldtimecard  联系不上成琳，先留此代码
                salesTimesCard.setValidDay(Integer.parseInt(oldTimeCard.getValidDay()));
//                salesTimesCard.setValidDay(Integer.parseInt(timeCardEntity.getValidDay()));

                TimesCardPurchaseDialogFragment timesCardPurchaseDialogFragment = new TimesCardPurchaseDialogFragment();
                Bundle bundle = new Bundle();
                bundle.putString("memberPhone", memberPhone);
                bundle.putSerializable("salesTimesCard", salesTimesCard);
                timesCardPurchaseDialogFragment.setArguments(bundle);
                timesCardPurchaseDialogFragment.show(getActivity().getSupportFragmentManager(), "timeCardPurchase");
                view.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        dismiss();
                    }
                },500);
                break;
        }
    }
}
